<?php

namespace app\admin\controller\car;

use app\common\controller\Backend;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\response\Json;
use wxApp\WxApp;

/**
 * 车辆管理-二手车
 *
 * @icon fa fa-circle-o
 */
class OldAuditing extends Backend
{

    /**
     * OldAuditing模型对象
     * @var \app\admin\model\car\OldAuditing
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\car\Old;
    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->field("car_old.*")
            ->field("u.name,u.nickname,u.mobile")
            ->join('user u','u.id = car_old.user_id')
            ->where($where)
            ->where("car_old.verify_status","<>",1)
            ->order($sort, $order)
            ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    //审核 通过
    public function shenhe()
    {

        $id = input("id");
        if (empty($id)){
            $this->error(__('Parameter %s can not be empty', ''));
        }

        Db::name("car_old")->where("id",$id)->update([
            "status"=>1,
            "verify_status"=>1,
            "verify_info"=>""
        ]);

        $car = Db::name("car_old")->where("id",$id)->find();
        $user = Db::name("user")->where("id",$car["user_id"])->find();
        $data_template = [
            "thing2" => ["value" => $car["car_models_name"]],
            "phrase1" => ["value" => "审核通过"],
            "time5" => ["value" => date("Y-m-d H:i:s")]
        ];
        (new WxApp)->sendMessage("-bKiYZzxx9pYKrjNDQ3q6a7xnhX7c3rmZ00wxpFL6j0",$user["openid"],$data_template);


        $this->success("审核成功");
    }

    /**
     * 批量通过
     *
     * @param $ids
     * @return void
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function shenhes($ids = null)
    {

        $ids = $ids ?: $this->request->post("ids/a");
        if (empty($ids)) {
            $this->error(__('Parameter %s can not be empty', 'ids'));
        }
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $list = $this->model->where($pk, 'in', $ids)->select();

        $count = 0;
        Db::startTrans();
        try {

            foreach ($list as $item) {
                Db::name("car_old")->where("id",$item["id"])->update([
                    "status"=>1,
                    "verify_status"=>1,
                    "verify_info"=>""
                ]);

                $car = Db::name("car_old")->where("id",$item["id"])->find();
                $user = Db::name("user")->where("id",$car["user_id"])->find();
                $data_template = [
                    "thing2" => ["value" => $car["car_models_name"]],
                    "phrase1" => ["value" => "审核通过"],
                    "time5" => ["value" => date("Y-m-d H:i:s")]
                ];
                (new WxApp)->sendMessage("-bKiYZzxx9pYKrjNDQ3q6a7xnhX7c3rmZ00wxpFL6j0",$user["openid"],$data_template);


            }
            Db::commit();
        } catch (PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($count) {
            $this->success("操作成功");
        }
        $this->error(__('No rows were deleted'));
    }


    //审核 驳回
    public function bohui()
    {

        $id = input("id");
        $info = input("verify_info");
        if (empty($id)&&empty($info)){
            $this->error(__('Parameter %s can not be empty', ''));
        }

        Db::name("car_old")->where("id",$id)->update([
            "verify_status"=>2,
            "verify_info"=>$info
        ]);

        $car = Db::name("car_old")->where("id",$id)->find();
        $user = Db::name("user")->where("id",$car["user_id"])->find();
        $data_template = [
            "thing2" => ["value" => $car["car_models_name"]],
            "phrase1" => ["value" => "审核被驳回"],
            "time5" => ["value" => date("Y-m-d H:i:s")]
        ];
        (new WxApp)->sendMessage("-bKiYZzxx9pYKrjNDQ3q6a7xnhX7c3rmZ00wxpFL6j0",$user["openid"],$data_template);

        $this->success("已驳回");
    }




    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
